// 全局权限工具函数
class Permission {
	// hasPermission
	hasPermission(permission) {
		return new Promise((resolve, reject) => {
			// 获取微信运动权限
			wx.getSetting({
				success: (res) => {
					if (res.authSetting[permission]) {
						resolve(true);
					} else {
						resolve(false);
					}
				},
				fail: (err) => {
					reject(err);
				},
			});
		});
	}
	// requestPermission
	requestPermission(permission) {
		return new Promise((resolve, reject) => {
			wx.authorize({
				scope: permission,
				success: (res) => {
					resolve(res);
				},
				fail: (err) => {
					reject(err);
				},
			});
		});
	}
	// openSetting
	openSetting() {
		return new Promise((resolve, reject) => {
			wx.openSetting({
				success: (res) => {
					resolve(res);
				},
				fail: (err) => {
					reject(err);
				},
			});
		});
	}
	// checkPermission **********************************************
	checkPermission(permission) {
		return new Promise((resolve, reject) => {
			if (!permission) reject("permission is null");

			this.hasPermission(permission)
				.then((res) => {
					console.log(res);
					if (res) {
						resolve(true);
					} else {
						this.requestPermission(permission)
							.then((res) => {
								resolve(true);
							})
							.catch((err) => {
								uni.showModal({
									title: "提示",
									content: "您未授权该权限，是否去设置授权？",
									success: (res) => {
										if (res.confirm) {
											this.openSetting()
												.then((res) => {
													resolve(true);
												})
												.catch((err) => {
													reject(err);
												});
										} else if (res.cancel) {
											reject(err);
										}
									},
								});
							});
					}
				})
				.catch((err) => {
					reject(err);
				});
		});
	}
	// getCode
	getCode() {
		return new Promise((resolve, reject) => {
			wx.login({
				success: (res) => {
					resolve(res);
				},
				fail: (err) => {
					console.log("登录失败！" + res.errMsg);
					reject(err);
				},
			});
		});
	}
	// getUserInfo,get avatarUrl,nickName,iv.....
	getUserInfo(title = "提示", content = "是否授权获取用户信息？") {
		return new Promise((resolve, reject) => {
			uni.showModal({
				title: title,
				content: content,
				success: (res) => {
					if (res.confirm) {
						uni.getUserProfile({
							desc: "将获取您的头像昵称",
							lang: "zh_CN",
							success(infores) {
								resolve(infores);
							},
							fail(err) {
								reject(err);
							},
						});
					} else if (res.cancel) {
						reject("取消授权1");
					}
				},
			});
		});
	}
	// getPhoneNumber
	getPhoneNumber() {
		return new Promise((resolve, reject) => {
			uni.showModal({
				title: "提示",
				content: "是否授权获取手机号？",
				success: (res) => {
					if (res.confirm) {
						uni.getPhoneNumber({
							success(infores) {
								resolve(infores);
							},
							fail(err) {
								reject(err);
							},
						});
					} else if (res.cancel) {
						reject(err);
					}
				},
			});
		});
	}
	// getWXRunData
	getWXRunData() {
		return new Promise((resolve, reject) => {
			uni.showModal({
				title: "提示",
				content: "是否授权获取微信运动数据？",
				success: (res) => {
					if (res.confirm) {
						uni.getWeRunData({
							success(infores) {
								resolve(infores);
							},
							fail(err) {
								reject(err);
							},
						});
					} else if (res.cancel) {
						reject(err);
					}
				},
			});
		});
	}
}

export default new Permission();
